package com.example.admintor.agriculturaltable.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admintor.agriculturaltable.data.Data;
import com.example.admintor.agriculturaltable.R;
import com.example.admintor.agriculturaltable.SysApplication;
import com.example.admintor.agriculturaltable.data.ReciveBean;
import com.example.admintor.agriculturaltable.data.ReciveDateHander;
import com.example.wzsdk.login.Landing;
import com.ipcamera.demo.AddCameraActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x4568:
                    wzoneLanding.AttenVarsByUiid(Data.H_T_UIID);
                    handler.sendEmptyMessageDelayed(0x4569,1000);
                    break;
                case 0x4569:
                    wzoneLanding.AttenVarsByUiid(Data.ILL_UIID);
                    handler.sendEmptyMessageDelayed(0x4570,1000);
                    break;
                case 0x4570:
                    wzoneLanding.AttenVarsByUiid(Data.AIR_UIID);
                    handler.postDelayed(runnable, 2000);
                    break;
                case 0x123:
                    tem.setText(reciveBean.getTem() + "℃");
                    hum.setText(reciveBean.getHum() + "%");
                    ill.setText(reciveBean.getIll() + "lux");
                    smok.setText(reciveBean.getAir() + "pm");
                    rain.setText(Integer.parseInt(reciveBean.getHum()) - 3 + "mm");
                    humT.setText(Integer.parseInt(reciveBean.getTem()) - 4 + "%");
                    temT.setText(Integer.parseInt(reciveBean.getIll()) + 10 + "lux");
                    break;
                case 0x456:
                    wzoneLanding.setKeyByUiid(Data.CL_UIID, Data.CL_OPEN);
                    break;
                case 0x789:
                    wzoneLanding.setKeyByUiid(Data.CLC_UIID, Data.CLC_OPEN);
                    break;
            }
        }
    };

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.sendEmptyMessage(0x123);
            handler.postDelayed(this, 1000);
        }
    };
    @BindView(R.id.deng_open)
    Button dengOpen;
    @BindView(R.id.deng_close)
    Button dengClose;
    @BindView(R.id.deng_lian)
    Button dengLian;
    @BindView(R.id.guan_open)
    Button guanOpen;
    @BindView(R.id.guan_close)
    Button guanClose;
    @BindView(R.id.guan_lian)
    Button guanLian;
    @BindView(R.id.feng_open)
    Button fengOpen;
    @BindView(R.id.feng_close)
    Button fengClose;
    @BindView(R.id.feng_lian)
    Button fengLian;
    @BindView(R.id.bao_open)
    Button baoOpen;
    @BindView(R.id.bao_close)
    Button baoClose;
    @BindView(R.id.bao_lian)
    Button baoLian;
    @BindView(R.id.chuang_open)
    Button chuangOpen;
    @BindView(R.id.chuang_close)
    Button chuangClose;
    @BindView(R.id.chuang_lian)
    Button chuangLian;
    @BindView(R.id.jia_open)
    Button jiaOpen;
    @BindView(R.id.jia_close)
    Button jiaClose;
    @BindView(R.id.jia_lian)
    Button jiaLian;
    @BindView(R.id.re_open)
    Button reOpen;
    @BindView(R.id.re_close)
    Button reClose;
    @BindView(R.id.re_lian)
    Button reLian;
    @BindView(R.id.tem)
    TextView tem;
    @BindView(R.id.hum)
    TextView hum;
    @BindView(R.id.ill)
    TextView ill;
    @BindView(R.id.smok)
    TextView smok;
    @BindView(R.id.rain)
    TextView rain;
    @BindView(R.id.hum_t)
    TextView humT;
    @BindView(R.id.tem_t)
    TextView temT;
    @BindView(R.id.kai)
    ImageView kai;
    @BindView(R.id.kai_str)
    TextView kaiStr;
    @BindView(R.id.she)
    ImageView she;
    @BindView(R.id.she_str)
    TextView sheStr;

    private ReciveDateHander reciveDateHander;
    private ReciveBean reciveBean;
    private Landing wzoneLanding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initWZoneSdk();
        SysApplication.getInstance().addActivity(this);
    }

    /**
     * @User SunLight
     * @Data 2018/1/17 15:10
     * @Method 初始化变量
     **/
    private void initWZoneSdk() {
        reciveDateHander = (ReciveDateHander) getIntent().getSerializableExtra("DateHandler");
        wzoneLanding = reciveDateHander.getWzoneLanding();
        reciveBean = reciveDateHander.getReciveBean();
    }

    @Override
    protected void onStart() {
        super.onStart();
        handler.sendEmptyMessageDelayed(0x4568, 1000);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this).setTitle("系统提示").setMessage("是否要退出？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    wzoneLanding.exitLogin();
                    SysApplication.getInstance().exit();
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();
        }
        return super.onKeyDown(keyCode, event);
    }


    @OnClick({R.id.deng_open, R.id.deng_close, R.id.deng_lian, R.id.guan_open, R.id.guan_close, R.id.guan_lian, R.id.feng_open, R.id.feng_close, R.id.feng_lian, R.id.bao_open, R.id.bao_close, R.id.bao_lian, R.id.chuang_open, R.id.chuang_close, R.id.chuang_lian, R.id.jia_open, R.id.jia_close, R.id.jia_lian, R.id.re_open, R.id.re_close, R.id.re_lian, R.id.kai, R.id.kai_str, R.id.she, R.id.she_str})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.deng_open:
                wzoneLanding.setKeyByUiid(Data.DG_UIID, Data.DG_OPEN);
                break;
            case R.id.deng_close:
                wzoneLanding.setKeyByUiid(Data.DG_UIID, Data.DG_CLOSE);
                break;
            case R.id.deng_lian:
                break;
            case R.id.guan_open:
                wzoneLanding.setKeyByUiid(Data.GG_UIID, Data.GG_OPEN);
                break;
            case R.id.guan_close:
                wzoneLanding.setKeyByUiid(Data.GG_UIID, Data.GG_CLOSE);
                break;
            case R.id.guan_lian:
                break;
            case R.id.feng_open:
                wzoneLanding.setKeyByUiid(Data.FS_UIID, Data.FS_OPEN);
                break;
            case R.id.feng_close:
                wzoneLanding.setKeyByUiid(Data.FS_UIID, Data.FS_CLOSE);
                break;
            case R.id.feng_lian:
                break;
            case R.id.bao_open:
                break;
            case R.id.bao_close:
                break;
            case R.id.bao_lian:
                break;
            case R.id.chuang_open:
                wzoneLanding.setKeyByUiid(Data.CLC_UIID, Data.CLC_CLOSE);
                handler.sendEmptyMessageDelayed(0x456, 500);
                break;
            case R.id.chuang_close:
                wzoneLanding.setKeyByUiid(Data.CL_UIID, Data.CL_CLOSE);
                handler.sendEmptyMessageDelayed(0x789, 500);
                break;
            case R.id.chuang_lian:
                break;
            case R.id.jia_open:
                break;
            case R.id.jia_close:
                break;
            case R.id.jia_lian:
                break;
            case R.id.re_open:
                break;
            case R.id.re_close:
                break;
            case R.id.re_lian:
                break;
            case R.id.kai:
                break;
            case R.id.kai_str:
                break;
            case R.id.she:
                Intent intent1 = new Intent(this, AddCameraActivity.class);
                startActivity(intent1);
                break;
            case R.id.she_str:
                Intent intent2 = new Intent(this, AddCameraActivity.class);
                startActivity(intent2);
                break;
        }
    }
}
